package meteo;

import java.util.Date;
import java.util.List;

import com.jksoft.meteo.client.WeatherReader;
import com.jksoft.meteo.data.WeatherEntry;
import com.jksoft.meteo.data.WeatherForecast;
import com.jksoft.meteo.util.WeatherException;

/**
 * Controlador del servicio de informacion meteorologica.
 * 
 * @author Jaime A. Corrales Gonzalez
 * 
 * @version 0.001a
 */
public class ControladorMeteo {

	public ControladorMeteo () {
		
	}
	
	public List<WeatherEntry> getForecast (String location) {
		try {
			WeatherReader reader = new WeatherReader(location);
			
			reader.process();

			WeatherForecast forecast = reader.getWeatherForecast();
			
			return forecast.getForecast();
		} catch (WeatherException e) {
			return null;
		}
	}
	
	public List<WeatherEntry> getForecastCribbed (String location, Date date) {
		try {
			WeatherReader reader = new WeatherReader(location);
			
			reader.process();

			WeatherForecast forecast = reader.getWeatherForecast();
			List<WeatherEntry> myForecast = forecast.getForecast();
			
			long todayTime = new Date().getTime();
			long dateTime = date.getTime();
			
			int diffDays = (int) (dateTime - todayTime) / (24 * 60 * 60 * 1000) + 1;

			if (diffDays > 3)
				return null;
			
			return myForecast.subList(diffDays, myForecast.size());
		} catch (WeatherException e) {
			return null;
		}
	}
}
